import { Component, OnInit } from '@angular/core';
import { ApplicationService } from "../../../../../service/application.service";
import { BladeContainerBase } from "../../../../../shared/blade/blade-container-base";
import { MenuShortcut } from "../../../../../entity/menu_shortcut.model";
import { Observable } from "rxjs/Observable";
import { Command } from "../../../../../shared/toolbar.service";
import { Blade } from "../../../../../shared/blade/blade.model";
import { BladeData } from "../../../../../shared/blade/blade-data";
import { DialogResult } from "../../../../../shared/dialog.service";


import { MenuShortcutService } from "../../../../../service/menu_shortcut.service";
import { MenuService } from "../../../../../service/menu.service";
import { Menu } from "../../../../../entity/menu.model";
import { MenuShortcutEditComponent } from "../menu-shortcut-edit/menu-shortcut-edit.component";


@Component({
  selector: 'app-menu-shortcut-list',
  templateUrl: './menu-shortcut-list.component.html',
  styleUrls: ['./menu-shortcut-list.component.css']
})
export class MenuShortcutListComponent extends BladeContainerBase<MenuShortcut>{

  /**
   * 所有菜单集合
   * 
   * @type {Menu[]}
   * @memberof MenuShortcutListComponent
   */
  menus: Menu[] = [];


  /**
   * 角色的固定菜单集合
   * 
   * @type {MenuShortcut[]}
   * @memberof MenuShortcutListComponent
   */
  menuShortCuts: MenuShortcut[] = [];


  /**
   * 当前固定菜单
   * 
   * @type {MenuShortcut[]}
   * @memberof MenuShortcutListComponent
   */
  currentMenuShortcut: MenuShortcut;

  constructor(
    private application: ApplicationService,
    private menuShortcutServer: MenuShortcutService,
    private menuServer: MenuService,
  ) {
    super()
  }
  ngOnInit() {
    this.commands = [
      new Command("edit", "编辑", "fa fa-edit", "setofkey:update", blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade)),

    ];
    super.ngOnInit();
  }


  initData(): any | Observable<any> | Promise<any> {

    let getMenus = this.menuServer.getMenus()
    let getMenuShortCuts = this.menuShortcutServer.getMenuShortCutsByroleId(this.parentEntity.roleId);
    Promise.all([getMenus, getMenuShortCuts])
      .then(res => {
        if (res[0]) {
          this.menus = [...res[0]];
        };
        if (res[1]) {
          this.menuShortCuts = [...res[1]];
        };
        //角色固定菜单
        this.menus.forEach(menu => {
          this.menuShortCuts.forEach(menuShortCut => {
            if (menu.menuId == menuShortCut.menuId) {
              menuShortCut.menuName = menu.menuName
            }
          })
        })

        this.onSeletedChange(this.menuShortCuts[0]);
      })
      .catch(error => this.bladeData.error = error)
      .then(res => this.bladeData.isLoading = false);

  }

  exeEdit(blend: Blade): void {
    if (this.currentMenuShortcut != undefined) {
      let data: BladeData = new BladeData();
      data.id = "menuShortcutEdit";
      data.title = this.currentMenuShortcut.menuName;
      let editRoleBlade = this.bladeService.createBlade(MenuShortcutEditComponent, data, false, this.currentMenuShortcut);
      this.currentBlade.bladeService.showBlade(editRoleBlade, this.currentBlade);
    }
  }
  canExeEdit(blend?: Blade): boolean {
    return this.currentMenuShortcut != null;
  }

  exeDelete(blend: Blade): void {
    let selectedKeys = this.getSelected();
    if (selectedKeys.length > 0) {
      this.dialogService
        .showConfirmationDialog("确认删除", "是否要删除选中项？")
        .subscribe(dialogResult => {
          if (dialogResult == DialogResult.Ok) {
            this.currentBlade.bladeData.isLoading = true;

            this.menuShortcutServer.batchDelete(selectedKeys.map(o => o.shortCutId))
              .then(res => {
                if (res) {
                  this.refresh(undefined);
                }
              })
              .catch(error => this.bladeData.error = error)
              .then(res => this.bladeData.isLoading = false);
          }
        })
    }

  }
  canDelete(blend?: Blade): boolean {
    return this.getSelected().length > 0;
  }

  /**
   * 单选
   * 
   * @param {HTMLInputElement} event 
   * @param {Role} key 
   * @memberof RoleListComponent
   */
  onItemCheack(event: HTMLInputElement, key: MenuShortcut) {
    key.isChecked = event.checked;
  }


  /**
   * 全选
   * 
   * @param {HTMLInputElement} event 
   * @memberof RoleListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.menuShortCuts.length > 0) {
      for (let i = 0; i < this.menuShortCuts.length; i++) {
        this.menuShortCuts[i].isChecked = event.checked;
      }
    }
  }

  /**
  * 获得选中项
  * 
  * @returns {Role[]} 
  * @memberof RoleListComponent
  */
  getSelected(): MenuShortcut[] {
    if (this.menuShortCuts.length > 0) {
      let filter = this.menuShortCuts.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }

  onSeletedChange(selectMenuShortcut: MenuShortcut) {
    if (this.currentBlade.childrenCanDeactivate()) {
      this.currentMenuShortcut = selectMenuShortcut;
    }

  }

}



// WEBPACK FOOTER //
// ./src/app/module/admin/role/menu-shortcut/menu-shortcut-list/menu-shortcut-list.component.ts